home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter25 / isohex25_2 / d3dfuncs.cpp next >
C/C++ Source or Header  |  2000-11-27  |  5KB  |  167 lines

  1. #include "d3dfuncs.h"
  2.  
  3. //create the IDirect3D7 interface
  4. LPDIRECT3D7 LPD3D_Create(LPDIRECTDRAW7 lpdd)
  5. {
  6.     LPDIRECT3D7 lpd3d;
  7.     //get the idirect3d pointer
  8.     lpdd->QueryInterface(IID_IDirect3D7,(void**)&lpd3d);//ICKY COM STUFF!
  9.     //return the pointer
  10.     return(lpd3d);
  11. }
  12.  
  13. //clean up an IDirect3D7
  14. void LPD3D_Release(LPDIRECT3D7* lpd3d)
  15. {
  16.     //safe release
  17.     if(*lpd3d)
  18.     {
  19.         (*lpd3d)->Release();
  20.         (*lpd3d)=NULL;
  21.     }
  22. }
  23.  
  24. //create the Device
  25. LPDIRECT3DDEVICE7 LPD3DDEV_Create(LPDIRECT3D7 lpd3d,LPDIRECTDRAWSURFACE7 lpdds)
  26. {
  27.     LPDIRECT3DDEVICE7 lpd3ddev;
  28.     //create the idirect3ddevice(hack method)
  29.     if(FAILED(lpd3d->CreateDevice(IID_IDirect3DTnLHalDevice,lpdds,&lpd3ddev)))//try tnl
  30.         if(FAILED(lpd3d->CreateDevice(IID_IDirect3DHALDevice,lpdds,&lpd3ddev)))//no tnl, try hal
  31.             if(FAILED(lpd3d->CreateDevice(IID_IDirect3DMMXDevice,lpdds,&lpd3ddev)))//no hal, try mmp
  32.                 if(FAILED(lpd3d->CreateDevice(IID_IDirect3DRGBDevice,lpdds,&lpd3ddev)))//no mmx, resort to rgb
  33.                     lpd3ddev=NULL;
  34.     //return the pointer
  35.     return(lpd3ddev);
  36. }
  37.  
  38. //set up the viewport
  39. void LPD3DDEV_SetViewport(LPDIRECT3DDEVICE7 lpd3ddev,DWORD x,DWORD y,DWORD width,DWORD height)
  40. {
  41.     //set up the view port
  42.     D3DVIEWPORT7 vp;
  43.     vp.dwX=x;
  44.     vp.dwY=y;
  45.     vp.dwWidth=width;
  46.     vp.dwHeight=height;
  47.     vp.dvMinZ=0.0;
  48.     vp.dvMaxZ=1.0;
  49.     //set the viewport for the device
  50.     lpd3ddev->SetViewport(&vp);
  51. }
  52.  
  53. //clear out the viewport
  54. void LPD3DDEV_Clear(LPDIRECT3DDEVICE7 lpd3ddev,D3DCOLOR color)
  55. {
  56.     //clear out the device
  57.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,color,0,0);
  58. }
  59.  
  60.  
  61. //draw triangle list
  62. void LPD3DDEV_DrawTriangleList(LPDIRECT3DDEVICE7 lpd3ddev,D3DTLVERTEX* pvertices,DWORD dwvertexcount)
  63. {
  64.     //draw the triangle list
  65.     lpd3ddev->DrawPrimitive(D3DPT_TRIANGLELIST,D3DFVF_TLVERTEX,pvertices,dwvertexcount,0);
  66. }
  67.  
  68. //draw triangle strip
  69. void LPD3DDEV_DrawTriangleStrip(LPDIRECT3DDEVICE7 lpd3ddev,D3DTLVERTEX* pvertices,DWORD dwvertexcount)
  70. {
  71.     //draw the triangle strip
  72.     lpd3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP,D3DFVF_TLVERTEX,pvertices,dwvertexcount,0);
  73. }
  74.  
  75. //clean up a device
  76. void LPD3DDEV_Release(LPDIRECT3DDEVICE7* lpd3ddev)
  77. {
  78.     //safe release
  79.     if(*lpd3ddev)
  80.     {
  81.         (*lpd3ddev)->Release();
  82.         (*lpd3ddev)=NULL;
  83.     }
  84. }
  85.  
  86. //primary surface as a 3d rendering target
  87. LPDIRECTDRAWSURFACE7 LPDDS_CreatePrimary3D(LPDIRECTDRAW7 lpdd,DWORD dwBackBufferCount)
  88. {
  89.     LPDIRECTDRAWSURFACE7 lpdds;
  90.     //create primary surface
  91.     DDSURFACEDESC2 ddsd;
  92.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  93.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  94.     ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  95.     ddsd.dwBackBufferCount=dwBackBufferCount;
  96.     ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_3DDEVICE;
  97.     lpdd->CreateSurface(&ddsd,&lpdds,NULL);
  98.     //return the surface pointer
  99.     return(lpdds);
  100. }
  101.  
  102. //back buffer as a 3d rendering target
  103. LPDIRECTDRAWSURFACE7 LPDDS_GetSecondary3D(LPDIRECTDRAWSURFACE7 lpdds)
  104. {
  105.     LPDIRECTDRAWSURFACE7 lpddsBack;
  106.     //create back buffer
  107.     DDSCAPS2 ddscaps;
  108.     memset(&ddscaps,0,sizeof(DDSCAPS2));
  109.     ddscaps.dwCaps=DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
  110.     lpdds->GetAttachedSurface(&ddscaps,&lpddsBack);
  111.     //return the new surface
  112.     return(lpddsBack);
  113. }
  114.  
  115. //create a texture
  116. LPDIRECTDRAWSURFACE7 LPDDS_CreateTexture(LPDIRECTDRAW7 lpdd,DWORD dwWidth,DWORD dwHeight)
  117. {
  118.     LPDIRECTDRAWSURFACE7 lpdds;
  119.     //create texture surface
  120.     DDSURFACEDESC2 ddsd;
  121.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  122.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  123.     ddsd.dwFlags=DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  124.     ddsd.dwWidth=dwWidth;
  125.     ddsd.dwHeight=dwHeight;
  126.     ddsd.ddsCaps.dwCaps=DDSCAPS_TEXTURE;
  127.     lpdd->CreateSurface(&ddsd,&lpdds,NULL);
  128.     //return the surface pointer
  129.     return(lpdds);
  130. }
  131.  
  132. //create a texture with a particular pixel format
  133. LPDIRECTDRAWSURFACE7 LPDDS_CreateTexturePixelFormat(LPDIRECTDRAW7 lpdd,DWORD dwWidth,DWORD dwHeight,LPDDPIXELFORMAT lpddpf)
  134. {
  135.     LPDIRECTDRAWSURFACE7 lpdds;
  136.     //create texture surface
  137.     DDSURFACEDESC2 ddsd;
  138.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  139.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  140.     ddsd.dwFlags=DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
  141.     ddsd.dwWidth=dwWidth;
  142.     ddsd.dwHeight=dwHeight;
  143.     ddsd.ddsCaps.dwCaps=DDSCAPS_TEXTURE;
  144.     memcpy(&ddsd.ddpfPixelFormat,lpddpf,sizeof(DDPIXELFORMAT));
  145.     lpdd->CreateSurface(&ddsd,&lpdds,NULL);
  146.     //return the surface pointer
  147.     return(lpdds);
  148. }
  149.  
  150. //set vertex data
  151. void VERTEX_Set(D3DTLVERTEX* pVert,D3DVALUE x,D3DVALUE y, D3DCOLOR color, D3DVALUE tu, D3DVALUE tv)
  152. {
  153.     //xyz
  154.     pVert->sx=x;
  155.     pVert->sy=y;
  156.     pVert->sz=0.5;
  157.     //rhw
  158.     pVert->rhw=1.0;
  159.     //color
  160.     pVert->color=color;
  161.     //specular
  162.     pVert->specular=0;
  163.     //texture coords
  164.     pVert->tu=tu;
  165.     pVert->tv=tv;
  166. }
  167.